﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPOI.HSSF.UserModel;
using System.Data;
using NPOI.SS.UserModel;
using UE.SharePoint.ImportExport.BLL.SharePoint;
using Microsoft.SharePoint;
using System.IO;
using UE.SharePoint.ImportExport.BLL.Excel;

namespace UE.SharePoint.ImportExport.BLL
{
    /// <summary>
    /// Represents a manager how export sharepoint list to extra file.
    /// </summary>
    public class ExportManager
    { 
        IReader _reader;
        IWritter _writer;

        /// <summary>
        /// Executes to export  
        /// </summary>
        /// <param name="spSite"></param>
        /// <param name="spWeb"></param>
        /// <param name="spListId"></param>
        /// <param name="fileName"></param>
        /// <param name="fileExt"></param>
        /// <returns></returns>
        public MemoryStream Execute(SPSite spSite, SPWeb spWeb, string spListId, string fileName, IStatus status, string query)
        {
            Setting setting = Init(spSite, spWeb, spListId, fileName, true, status, query);
             
            DataTable exportData = _reader.GetData();
            _writer.Writer(exportData);
            return setting.ResponseMemory;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="spSite"></param>
        /// <param name="spWeb"></param>
        /// <param name="spListId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private Setting Init(SPSite spSite, SPWeb spWeb, string spListId, string fileName, bool containAdditionalColumns, IStatus status, string query)
        {
            Setting setting = new Setting();
            setting.SPSite = spSite;
            setting.SPWeb = spWeb;
            setting.FileName = setting.FileName;
            setting.Query = query;
            setting.IsContainAdditionalColumn = containAdditionalColumns;
            setting.SPList = setting.SPWeb.Lists[new Guid(spListId)];
            setting.ResponseMemory = new MemoryStream(); 
            _reader = new SPListReader(setting);
            _writer = new XLSWriter(setting, status);

            return setting;
        } 

        /// <summary>
        /// Executes to export  
        /// </summary>
        /// <param name="spSite"></param>
        /// <param name="spWeb"></param>
        /// <param name="spListId"></param>
        /// <param name="fileName"></param>
        /// <param name="fileExt"></param>
        /// <returns></returns>
        public MemoryStream ExportSchema(SPSite spSite, SPWeb spWeb, string spListId, string fileName,bool containAdditionalColumns,IStatus status)
        {
            Setting setting = Init(spSite, spWeb, spListId, fileName, containAdditionalColumns, status,string.Empty);

            DataTable exportData = new DataTable();
            exportData.TableName = setting.SPList.Title; 
            Column[] columns = _reader.LoadColumns();
            foreach (Column column in columns)
            {
                exportData.Columns.Add(new DataColumn(column.Name));
            }

            _writer.Writer(exportData);
            return setting.ResponseMemory;
        } 
    }
}